﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace XrmLibrary.ConfigFile
{
    [ConfigurationCollection(typeof(AdapterInstanceCollection), AddItemName=AdapterCollection.ItemName, CollectionType=ConfigurationElementCollectionType.BasicMap)]
    public class AdapterCollection : ConfigurationElementCollection
    {
        private const String ItemName = "instance";

        private static ConfigurationPropertyCollection _properties;

        #region Constructors
        static AdapterCollection()
        {
            _properties = new ConfigurationPropertyCollection();
        }

        public AdapterCollection()
        {}
        #endregion

        #region Properties
        protected override ConfigurationPropertyCollection Properties
        {
            get { return _properties; }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return ItemName; }
        }
        #endregion


        #region Indexers
        public AdapterInstanceCollection this[Int32 index]
        {
            get { return (AdapterInstanceCollection)base.BaseGet(index); }
            set
            {
                if(base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }


        public AdapterInstanceCollection this[String name]
        {
            get { return (AdapterInstanceCollection) base.BaseGet(name); }
        }
        #endregion


        protected override ConfigurationElement CreateNewElement()
        {
            return new AdapterInstanceCollection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as AdapterInstanceCollection).Name;
        }

        // This was necessary to add custom attributes to the adapter collection.
        protected override bool OnDeserializeUnrecognizedAttribute(string name, string value)
        {
            if(name == AssemblyNameName)
            {
                this.AssemblyName = value;
                return true;
            }
            else if(name == AssemblyPathName)
            {
                this.AssemblyPath = value;
                return true;
            }
            return base.OnDeserializeUnrecognizedAttribute(name, value);
        }

        private const String AssemblyNameName = "assemblyName";
        [ConfigurationProperty(AssemblyNameName, DefaultValue = "", IsKey = true, IsRequired = true)]
        public String AssemblyName
        {
            get { return (String)_assemblyName; }
            set { this._assemblyName = value; }
        }

        private String _assemblyName;

        private const String AssemblyPathName = "assemblyPath";
        [ConfigurationProperty(AssemblyPathName, DefaultValue = "", IsKey = false, IsRequired = true)]
        public String AssemblyPath
        {
            get { return this._assemblyPath; }
            set { this._assemblyPath = value; }
        }

        private String _assemblyPath;

    }
}
